package com.xunchi.haichuan.user.service;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.ArrayUtils;

import com.xunchi.haichuan.user.dao.PermissionDAO;
import com.xunchi.haichuan.user.dao.UserDAO;
import com.xunchi.haichuan.user.dao.UserDAOImpl;
import com.xunchi.haichuan.user.dao.UserPermissionDAO;
import com.xunchi.haichuan.user.dao.UserPermissionDAOImpl;
import com.xunchi.haichuan.user.model.User;
import com.xunchi.haichuan.user.model.UserPermission;

/**
 * 作者：kongxb<br>
 * 模块名称：用户模块<br>
 * 功能描述：用户服务实现<br>
 * 维护记录：
 */
public class UserServiceImpl implements UserService {

	/**
	 * 用户数据存取DAO
	 */
	private UserDAO userDAO;
	/**
	 * 用户权限数据存取DAO
	 */
	private UserPermissionDAO userPermissionDAO;
	/**
	 * 权限数据存取DAO
	 */
	private PermissionDAO permissionDAO;

	private int totalCount;

	/**
	 * 构造函数
	 */
	public UserServiceImpl() {
		userDAO = new UserDAOImpl(User.class);
		userPermissionDAO = new UserPermissionDAOImpl(UserPermission.class);
	}

	/**
	 * @see com.xunchi.haichuan.user.service.UserService#getUser(java.lang.String, java.lang.String)
	 */
	public User getUser(String loginName, String loginPassword) throws Exception {
		return userDAO.getUser(loginName, loginPassword);
	}

	/**
	 * @see com.xunchi.haichuan.user.service.UserService#getUser(java.lang.String, java.lang.String, int)
	 */
	public User getUser(String loginName, String loginPassword, int type) throws Exception {
		return userDAO.getUser(loginName, loginPassword, type);
	}

	/**
	 * @see com.xunchi.haichuan.user.service.UserService#addUser(java.lang.String, java.lang.String, java.lang.String,
	 *      java.lang.String, int, int)
	 */
	public void addUser(String loginName, String loginPassword, String userName, String email, int status, int type)
			throws Exception {
		User user = new User();
		user.setLoginName(loginName);
		user.setLoginPassword(loginPassword);
		user.setName(userName);
		user.setEmail(email);
		user.setStatus(status);
		user.setType(type);
		userDAO.saveEntity(user);
	}

	/**
	 * @see com.xunchi.haichuan.user.service.UserService#checkLoginName(java.lang.String)
	 */
	public boolean checkLoginName(String loginName) throws Exception {
		return userDAO.checkLoginName(loginName);
	}

	/**
	 * @see com.xunchi.haichuan.user.service.UserService#addUserPermission(java.lang.String, java.lang.String)
	 */
	public void addUserPermission(String usrId, String permissionId) throws Exception {
		UserPermission userPermission = new UserPermission();
		userPermission.setUsrId(usrId);
		userPermission.setPermissionId(permissionId);
		userPermissionDAO.saveEntity(userPermission);
	}

	/**
	 * @see com.xunchi.haichuan.user.service.UserService#deleteUserPermission(java.lang.String, java.lang.String)
	 */
	public void deleteUserPermission(String usrId, String permissionId) throws Exception {
		userPermissionDAO.delete(usrId, permissionId);
	}

	/**
	 * @see com.xunchi.haichuan.user.service.UserService#getByType(java.lang.Integer)
	 */
	public List<User> getByType(int type) throws Exception {
		return userDAO.getByType(type);
	}

	/**
	 * @see com.xunchi.haichuan.user.service.UserService#getChildren(java.lang.String)
	 */
	public List<User> getChildren(String parentId) throws Exception {
		return userDAO.getByParentId(parentId);
	}

	/**
	 * @see com.xunchi.haichuan.user.service.UserService#changeStatus(java.lang.String, java.lang.Integer)
	 */
	public void changeStatus(String usrId, int status) throws Exception {
		if (status == 1) {
			if (!userPermissionDAO.checkPermission(usrId, "searchData")) {
				addUserPermission(usrId, "searchData");
			}
			if (!userPermissionDAO.checkPermission(usrId, "exportData")) {
				addUserPermission(usrId, "exportData");
			}
		} else if (status == 0) {
			if (userPermissionDAO.checkPermission(usrId, "searchData")) {
				deleteUserPermission(usrId, "searchData");
			}
			if (userPermissionDAO.checkPermission(usrId, "exportData")) {
				deleteUserPermission(usrId, "exportData");
			}
		}

		userDAO.changeStatus(usrId, status);
	}

	/**
	 * @see com.xunchi.haichuan.user.service.UserService#changePwd(java.lang.String, java.lang.String, java.lang.String)
	 */

	public int changePwd(String usrId, String password, String oldPassword) throws Exception {
		return userDAO.changePwd(usrId, password, oldPassword);
	}

	/**
	 * @see com.xunchi.haichuan.user.service.UserService#charge(java.lang.String, java.lang.Float)
	 */
	public void charge(String usrId, float diff) throws Exception {
		User user = this.getUserById(usrId);
		userDAO.changeBalance(usrId, user.getBalance() + diff);
	}

	/**
	 * @see com.xunchi.haichuan.user.service.UserService#getUserById(java.lang.String)
	 */
	public User getUserById(String usrId) throws Exception {
		return userDAO.getByID(usrId);
	}

	/**
	 * @see com.xunchi.haichuan.user.service.UserService#checkPermission(java.lang.String, java.lang.String)
	 */
	public boolean checkPermission(String usrId, String permissionId) throws Exception {
		return userPermissionDAO.checkPermission(usrId, permissionId);
	}

	/**
	 * @see com.xunchi.haichuan.user.service.UserService#getPermissions(java.lang.String)
	 */
	public List<String> getPermissions(String usrId) throws Exception {
		return userPermissionDAO.getPermissions(usrId);
	}

	/**
	 * @see com.xunchi.haichuan.user.service.UserService#search(java.lang.Object[], java.util.Date, java.util.Date)
	 */
	public List<User> search(Object[] params, Date beginDate, Date endDate) throws Exception {
		User instance = new User();
		if (params.length == 5) {
			instance.setName((String) params[0]);
			instance.setLoginName((String) params[1]);
			instance.setStatus((Integer) params[2]);
			instance.setParentId((String) params[3]);
			instance.setType((Integer) params[4]);
		}

		List<User> userList = userDAO.search(instance, beginDate, endDate);

		totalCount = userList.size();
		sortUserList(userList); // 按照提交时间逆序排序
		return userList;
	}

	/**
	 * @see com.xunchi.haichuan.user.service.UserService#search(java.lang.Object[],java.util.Date, java.util.Date, int,
	 *      int)
	 */
	public List<User> search(Object[] params, Date beginDate, Date endDate, int start, int limit) throws Exception {
		List<User> userList = search(params, beginDate, endDate);
		sortUserList(userList); // 按照提交时间逆序排序
		return Arrays.asList((User[]) ArrayUtils.subarray(userList.toArray(new User[] {}), start, start + limit));
	}

	/**
	 * @see com.xunchi.haichuan.user.service.UserService#sortUserList(java.util.List<User>)
	 */
	public void sortUserList(List<User> userList) {
		// 按照提交时间逆序排序
		Collections.sort(userList, Collections.reverseOrder(new Comparator() {
			public int compare(Object o1, Object o2) {
				User f1 = (User) o1;
				User f2 = (User) o2;
				return (f1.getSubmitTime().compareTo(f2.getSubmitTime()));
			}
		}));
	}

	/**
	 * @see com.xunchi.haichuan.user.service.UserService#getTotalCount()
	 */
	public int getTotalCount() {
		return this.totalCount;
	}

}
